package com.ly.cloud.controller;

import java.util.List;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.GroupMembersDto;
import com.ly.cloud.service.GroupMembersService;
import com.ly.cloud.vo.GroupMembersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ly.cloud.dto.UserGroupDto;
import com.ly.cloud.service.UserGroupService;
import com.ly.cloud.vo.UserGroupVo;
import com.ly.cloud.web.utils.WebResponse;

import io.swagger.annotations.Api;
/**
 * 用户组
 * @author pengshiqing
 *
 */
@RestController
@RequestMapping("/dataPermission")
@Api("用户权限Controller")
public class DataPermissionController {
	
	@Autowired
	private UserGroupService userGroupService;

	@Autowired
	GroupMembersService groupMembersService;

	/**
	 * 新增
	 * @param dto
	 */
	@RequestMapping(value = "/userGroup/insert", method = RequestMethod.POST)
	public WebResponse<Integer> insertUserGroup(@RequestBody UserGroupDto dto , @RequestHeader(required=false,value = "loginUserName") String loginUserName) {
		int result = userGroupService.insert(dto,loginUserName) ;
		return new WebResponse<Integer>().success(result);
	}
	
	/**
	 * 修改
	 * @param dto
	 */
	@RequestMapping(value = "/userGroup/updateById", method = RequestMethod.POST)
	public WebResponse<Integer> updateUserGroupById(@RequestBody UserGroupDto dto , @RequestHeader(required=false,value = "loginUserName") String loginUserName) {
		int result = userGroupService.updateById(dto,loginUserName) ;
		return new WebResponse<Integer>().success(result);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/userGroup/deleteById/{id}", method = RequestMethod.POST)
	public WebResponse<Integer> deleteUserGroupById(@PathVariable Long id) {
		int result = userGroupService.deleteById(id);
		return new WebResponse<Integer>().success(result);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/userGroup/deleteByIds", method = RequestMethod.POST)
	public WebResponse<Integer> deleteUserGroupByIds(@RequestBody List<Long> ids) {
		int result = userGroupService.deleteByIds(ids);
		return new WebResponse<Integer>().success(result);
	}

	/**
	 * 树形结构  根据父节点查询所有子节点
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/userGroup/tree", method = RequestMethod.GET)
	public WebResponse<List<UserGroupVo>> findByUserGroupParentId(@RequestParam(required = false,value = "id") Long id){
		List<UserGroupVo> listVo = userGroupService.findByParentId(id);
		return new WebResponse<List<UserGroupVo>>().success(listVo);
	}


	/**
	* @Description: 查询
	* @Author: pengshiqing
	* @CreateDate: 2018/12/24
	*/
	@RequestMapping(value = "/groupMembers/queryPageList", method = RequestMethod.GET)
	public WebResponse<PageInfo<GroupMembersVo>> queryUserGroupPageList(@RequestParam("groupid") Long groupid ,
                                                                        @RequestParam("pageNum") int pageNum,
                                                                        @RequestParam("pageSize") int pageSize,
                                                                        GroupMembersDto dto){

		Page<GroupMembersVo> page = groupMembersService.selectPage(new Page<GroupMembersVo>(pageNum, pageSize), groupid, dto);
		return new WebResponse<PageInfo<GroupMembersVo>>().success(PageInfo.toPageInfo(page));

	}

	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/groupMembers/deleteById/{id}", method = RequestMethod.POST)
	public WebResponse<Integer> deleteGroupMembersById(@PathVariable Long id){
		int result = groupMembersService.deleteById(id) ;
		return new WebResponse<Integer>().success(result);
	}

	/**
	 * 根据id批量删除用户
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/groupMembers/deleteByIds", method = RequestMethod.POST)
	public WebResponse<Integer> deleteGroupMembersByIds(@RequestBody List<Long> ids){
		int result = groupMembersService.deleteByIds(ids) ;
		return new WebResponse<Integer>().success(result);
	}

	/**
	 * 插入用户
	 * @param groupid
	 * @param loginUserName
	 * @param jghs
	 * @return
	 */
	@RequestMapping(value = "/groupMembers/insert", method = RequestMethod.POST)
	public WebResponse<Boolean> insertGroupMembers(@RequestParam("groupid") Long groupid ,@RequestHeader(required=false,value = "loginUserName") String loginUserName,@RequestBody List<String> jghs){
		groupMembersService.insert(groupid,loginUserName,jghs);
		return new WebResponse<Boolean>().success(true) ;
	}


	/**
	 * 点击新增后弹窗  条件+分页 查询不在当前用户组下的所有用户
	 * @param pageNum
	 * @param pageSize
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/groupMembers/showUserList", method = RequestMethod.GET)
	public WebResponse<PageInfo<GroupMembersVo>> showGroupMembersList(@RequestParam("groupid") Long groupid ,
                                                                      @RequestParam("pageNum") int pageNum,
                                                                      @RequestParam("pageSize") int pageSize,
                                                                      GroupMembersDto dto){

		Page<GroupMembersVo> page = groupMembersService.showUserListPage(new Page<GroupMembersVo>(pageNum, pageSize), groupid, dto);
		return new WebResponse<PageInfo<GroupMembersVo>>().success(PageInfo.toPageInfo(page));

	}

    /**
     * @Description: 查询主编号是否存在
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @RequestMapping(value = "/checkedZbh", method = RequestMethod.GET)
    public WebResponse<Boolean> checkedZbh(@RequestParam("zbh") Long zbh){
        return new WebResponse<Boolean>().success(userGroupService.checkedZbh(zbh));
    }


}
